/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.ubc.icapture.genapha.actions;

import ca.ubc.icapture.genapha.forms.AnalysisFilesForm;
import ca.ubc.icapture.genapha.forms.ExportRawGenotypesForm;
import icapture.SQLMgr;
import icapture.beans.DB.User;
import icapture.beans.DBExportOptions;
import icapture.beans.Format;
import icapture.genapha.GenotypeToolsManager;
import icapture.genapha.exporters.V2SampleExporter;

/**
 *
 * @author btripp
 */
public class ExportRawGenotypesAction extends AnalysisFilesAction
{

    @Override
    protected void processRequest(AnalysisFilesForm form)
    {
        final ExportRawGenotypesForm rawForm = (ExportRawGenotypesForm) form;
        final DBExportOptions options = new DBExportOptions();
        GenotypeToolsManager gtm = new GenotypeToolsManager();
        Format format;

        // Add name to the result
        result.setName("Raw genotypes export");
        SQLMgr.updateResult(result);

        // Set format
        if (rawForm.getFormat().equalsIgnoreCase("fbat"))
        {
            format = gtm.FBAT_FORMAT;
        }
        else
        {
            format = null;
            // TODO: Find out which format is Plink
        }

        // Setup the snp arrays
        String[] snps = null;
        if (rawForm.getSnps() != null)
        {
            snps = rawForm.getSnps().trim().split("\n");
            for (String snp : snps)
            {
                snp = snp.trim();
            }
        }

        options.setCohortIds(form.getSelectedCohort());
        options.setPhenotypeID(Integer.valueOf(form.getSelectedPhenotype()));
        options.setResult(result);
        options.setFormat(format);
        User user = new User();
        user.setEmail(form.getEmail());
        options.setUser(user);
        options.setSelectedGenotypingRuns(rawForm.getSelectedGenotypingRun());
        options.setRsNumbers(snps);
        options.setChromosomes(rawForm.getChromosomes());

        if (form.getSelectedCohort().length > 0)
        {
            V2SampleExporter sampleExporter = new V2SampleExporter(options);
            sampleExporter.export();
        }
    }
}
